package com.wlanauth.web.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.SystemProperty;
import com.wlanauth.service.manage.SystemPropertyService;
import com.wlanauth.web.core.BaseActionSupport;
import com.wlanauth.web.core.JsonResult;
import com.wlanauth.web.util.SystemPropertyUtils;
/**
 * 系统
 * @author zz
 * @date 2012-5-10
 * @email charles.zz.life@gmail.com
 */
public class SystemPropertyAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;

	private SystemProperty systemProperty;
	private SystemPropertyService systemPropertyService;
	
	private List<SystemProperty> properties;
	
	public String save() {
		this.systemProperty.setUpdateTime(new Date());
		if(StringUtils.isBlank(systemProperty.getKey())){
			this.systemPropertyService.save(this.systemProperty);
			SystemPropertyUtils.flushProperty(this.systemProperty);
		} else {
			SystemProperty temp = this.systemPropertyService.get(systemProperty.getKey());
			temp.setValue(this.systemProperty.getValue());
			temp.setUpdateTime(this.systemProperty.getUpdateTime());
			this.systemPropertyService.update(temp);
			SystemPropertyUtils.flushProperty(temp);
		}
		this.jsonResult = new JsonResult(this,true);
		return this.actionJsonResult();
	}
	
	public String saveProperties(){
		if(this.properties==null){
			this.jsonResult = new JsonResult(this,"请检查参数",false);
			return this.actionJsonResult();
		}
		for(SystemProperty p:properties){
			p.setUpdateTime(new Date());
			if(StringUtils.isBlank(p.getKey())){
				this.systemPropertyService.save(p);
				SystemPropertyUtils.flushProperty(p);
			} else {
				SystemProperty temp = this.systemPropertyService.get(p.getKey());
				temp.setValue(p.getValue());
				temp.setUpdateTime(p.getUpdateTime());
				this.systemPropertyService.update(temp);
				SystemPropertyUtils.flushProperty(temp);
			}
		}
		this.jsonResult = new JsonResult(this,true);
		return this.actionJsonResult();
	}

	public String findKey() {
		SystemProperty prop = SystemPropertyUtils.getProperty(this.systemProperty.getKey());
		this.jsonResult = new JsonResult(this,prop,true);
		return this.actionJsonResult();
	}
	
	public String getAllProperties() {
		GeneralCriteria criteria = new GeneralCriteria(SystemProperty.class);
		criteria.setDataOnly(true);
		GeneralCriteriaResult<SystemProperty> result = criteria.executeQuery();
		this.jsonResult = new JsonResult(this,result.getData(),result.getTotal());
		return this.actionJsonResult();
	}

	public SystemProperty getSystemProperty() {
    	return this.systemProperty;
    }

	public void setSystemProperty(SystemProperty systemProperty) {
    	this.systemProperty = systemProperty;
    }

	public SystemPropertyService getSystemPropertyService() {
    	return this.systemPropertyService;
    }

	public void setSystemPropertyService(SystemPropertyService systemPropertyService) {
    	this.systemPropertyService = systemPropertyService;
    }

	public List<SystemProperty> getProperties() {
    	return this.properties;
    }

	public void setProperties(List<SystemProperty> properties) {
    	this.properties = properties;
    }
	

}
